package com.example.newstudent.controller;

import com.example.newstudent.entity.*;
import com.example.newstudent.service.studentService;
import org.apache.ibatis.annotations.Update;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import static com.example.newstudent.entity.lesson.summarizecourses;

@CrossOrigin
@RestController
@RequestMapping("/student")
public class studentController {
    @Autowired
    studentService studentService;
    @PostMapping("/Login")
    public Result Login(@RequestBody student student){
        System.out.println("学号为"+student.getId()+"的同学登录");
        student login = studentService.login(student);
        if (login != null) {
            System.out.println("登陆成功");
            return Result.success(login);
        }else{
            System.out.println("登录失败");
            return Result.error("密码错误");
        }
    }
    @PostMapping("/enroll")
    public Result enroll(@RequestBody student student){
        System.out.println("学号为"+student.getId()+"同学注册");
        if (student.getId().isEmpty()
                ||student.getPin().isEmpty()
                ||student.getName().isEmpty()
                || student.getProvinces().isEmpty()
                ||student.getTelephone().isEmpty()) {
            return Result.error("表单项不能为空");
        }
        if (studentService.enroll(student) == 0) {
            return Result.error("该学号同学已经注册");
        }else{

            return Result.success("注册成功");
        }
    }
    @GetMapping("/Getlesson")
    public Result Getlesson(@RequestParam  String id,@RequestParam int page){
        System.out.println("学号为"+ id +"的同学查看课表");
        List<lesson> lessons = studentService.getlesson(id,page);
        return Result.success(lessons);
    }
    @PostMapping("/status")
    public Result status(@RequestBody student student){
        System.out.println("学号为"+student.getId()+"的同学进行报道");
        studentService.status(student.getId());
        return Result.success();
    }
    @GetMapping("/selectLesson")
    public Result selectLesson(@RequestParam String id,@RequestParam String name){
        System.out.println("学号为"+id+"的同学选课"+name);
        studentService.selectLesson(id,name);
        return Result.success();
    }
    @PostMapping("/updatemessage")
    public Result updatemessage(@RequestBody student student){
        System.out.println("学号为"+student.getId()+"的同学修改信息");
        if (student.getPin().isEmpty()
                ||student.getName().isEmpty()
                || student.getProvinces().isEmpty()
                ||student.getTelephone().isEmpty() ) {
                return Result.error("表单项不能为空");
        }else
        return  Result.success(studentService.updatemessage(student));
    }
    @PostMapping("/haveSelect")
    public Result haveSelect(@RequestBody student student){
        List<lesson> lessonList = new ArrayList<>();
        System.out.println("学号为"+student.getId()+"的同学查看已选课表");
        List<lesson> lessons = studentService.haveSelect(student.getId());
        Map<String, Map<String, Object>> weeksCount = summarizecourses(lessons);
        for (Map.Entry<String, Map<String, Object>> entry : weeksCount.entrySet()) {
            lesson optional_course = new lesson();
            optional_course.setName(entry.getKey());
            optional_course.setTeacher((String) entry.getValue().get("teacher"));
            optional_course.setWeeks((String) entry.getValue().get("weeks"));
            optional_course.setAddress((String) entry.getValue().get("address"));
            lessonList.add(optional_course);
        }
        return Result.success(lessonList);
    }
@GetMapping("/withDrawal")
    public Result withDrawal(@RequestParam String id,@RequestParam String name){
    System.out.println("学号为"+id+"的同学退课"+name);
    studentService.withDrawal(id,name);
    return Result.success();
}
}
